package org.ezest.masteringextjs.dao.impl;

import java.util.List;

import org.ezest.masteringextjs.dao.UserDao;
import org.ezest.masteringextjs.model.User;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

public class UserDaoImpl implements UserDao{
	static SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
	
	@Override
	public User findByUserName(String userName) {
		// TODO Auto-generated method stub
		
		Session session=sessionFactory.openSession();
		session.beginTransaction();
		
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("userName", userName));
		
		List<User> actList = criteria.list();
		session.getTransaction().commit();
		session.close();
		
		if(actList.isEmpty()) {
			return null;
		} else {
			return actList.get(0);
		}
	}
	
	public static void main(String[] args) {
		Session session=sessionFactory.openSession();
		session.beginTransaction();
		User user = new User();
		user.setEmain("asdfasfd");
		user.setPassword("e10adc3949ba59abbe56e057f20f883e");
		user.setUserName("loiane");
		session.save(user);
		session.getTransaction().commit();
		session.close();
	}

}
